package com.mediacloud.app.newsmodule.activity

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.mediacloud.app.model.activity.BaseBackActivity
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.adaptor.Component105type3ActivityAdapter
import com.mediacloud.app.newsmodule.drama.AnchorHeatRankList
import com.mediacloud.app.newsmodule.fragment.baoliao.api.SpiderCmsApi
import com.mediacloud.app.newsmodule.utils.SBShareUtils
import com.mediacloud.app.newsmodule.utils.share.ShareWrap
import com.utils.RxUtils
import kotlinx.android.synthetic.main.activity_component105type3.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class Component105Type3Activity : BaseBackActivity(), RxUtils.OnClickInterf {

    override fun getFitSystemWindow(): Boolean = false

    override fun getStatusBarColor(): Int = Color.TRANSPARENT
    override fun getLayoutResID(): Int {
        return R.layout.activity_component105type3
    }

    private var adapter: Component105type3ActivityAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        tv_title?.text = "达人榜"
        adapter = Component105type3ActivityAdapter()
        recyclerView?.layoutManager = LinearLayoutManager(this)
        recyclerView?.adapter = adapter
        img_bg?.post {
            nestedScrollView?.setOnScrollChangeListener(object :
                View.OnScrollChangeListener {
                override fun onScrollChange(
                    v: View?,
                    scrollX: Int,
                    scrollY: Int,
                    oldScrollX: Int,
                    oldScrollY: Int
                ) {
                    if (scrollY > img_bg.height / 2) {
                        if (img_back?.colorFilter == null) {
                            img_back?.setColorFilter(Color.BLACK)
                            img_more?.setColorFilter(Color.BLACK)
                            view_mask?.setBackgroundColor(Color.WHITE)
                            tv_title?.visibility = View.VISIBLE
                        }
                    } else {
                        if (img_back?.colorFilter != null) {
                            img_back?.clearColorFilter()
                            img_more?.clearColorFilter()
                            view_mask?.setBackgroundColor(Color.TRANSPARENT)
                            tv_title?.visibility = View.GONE
                        }
                    }
                }
            })
        }

        RxUtils.rxClick(img_back, this)
        RxUtils.rxClick(net_error, this)
        RxUtils.rxClick(img_more, this)
    }

    override fun onResume() {
        super.onResume()
        getData()
    }

    override fun onViewClick(view: View?) {
        when (view?.id) {
            R.id.img_back -> {
                finish()
            }
            R.id.net_error -> {
                getData()
            }
            R.id.img_more -> {
                var shareWrap = ShareWrap(this, false)
                shareWrap?.setShowFunction(false)
                //val articleItem = ArticleItem()
                shareWrap?.init(null, null)
                shareWrap?.removeGridItem(SBShareUtils.SharePoster, SBShareUtils.CopyLink)
                shareWrap?.show(rootView)
            }
        }
    }

    fun getData() {
        SpiderCmsApi.getSpiderServerApi(this).anchorHeatRankList(10)
            .enqueue(object : Callback<AnchorHeatRankList> {
                override fun onResponse(
                    call: Call<AnchorHeatRankList>,
                    response: Response<AnchorHeatRankList>
                ) {
                    try {
                        if (response.body() != null && response.body()?.state == 200 && response.body()?.data != null) {
                            net_error?.visibility = View.GONE
                            adapter?.setNewData(response.body()?.data?.list)
                            tv_time?.text =
                                "更新时间：" + response.body()?.data?.updateTime?.split(" ")?.get(0)
                        } else {
                            net_error?.visibility = View.VISIBLE
                        }
                    } catch (e: Exception) {

                    }

                }

                override fun onFailure(call: Call<AnchorHeatRankList>, t: Throwable) {
                    net_error?.visibility = View.VISIBLE
                }
            })
    }

}